import { objectType, extendType, stringArg } from "@nexus/schema";

export const File = objectType({
  name: "File",
  definition(t) {
    t.string("id");
    t.string("name");
    t.string("extension", { nullable: true }); // file extension name
    t.string("content", { nullable: true }); // file content,code or text
    t.field("folder", { nullable: true, type: "Folder" });
  },
});

export const FileQuery = extendType({
  type: "Query",
  definition(t) {
    t.field("file", {
      type: "File",
      args: {
        fileId: stringArg({ required: true }),
      },
      resolve(__root, args, ctx) {
        return ctx.db.file.findOne({
          where: { id: args.fileId },
          include: {
            folder: true,
          },
        });
      },
    });
  },
});

export const FilesQuery = extendType({
  type: "Query",
  definition(t) {
    t.field("files", {
      type: "File",
      list: true,
      resolve(__root, args, ctx) {
        return ctx.db.file.findMany({
          include: {
            folder: true,
          },
        });
      },
    });
  },
});

export const CreateFileMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.field("createFile", {
      type: "File",
      args: {
        name: stringArg({ required: true }),
        extension: stringArg(),
        content: stringArg(),
        folderId: stringArg({ required: true }),
      },
      resolve(_root, args, ctx) {
        if (args.extension === null || args.extension === undefined) {
          if (args.content === null || args.content === undefined) {
            const file = {
              name: args.name,
              extension: null,
              content: null,
              folder: { connect: { id: args.folderId } },
            };
            return ctx.db.file.create({ data: file });
          } else {
            const file = {
              name: args.name,
              extension: null,
              folder: { connect: { id: args.folderId } },
            };
            return ctx.db.file.create({ data: file });
          }
        } else {
          if (args.content === null || args.content === undefined) {
            const file = {
              name: args.name,
              extension: args.extension,
              content: null,
              folder: { connect: { id: args.folderId } },
            };
            return ctx.db.file.create({ data: file });
          } else {
            const file = {
              name: args.name,
              extension: args.extension,
              content: args.content,
              folder: { connect: { id: args.folderId } },
            };
            return ctx.db.file.create({ data: file });
          }
        }
      },
    });
  },
});

export const DeleteFileMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.field("deleteFile", {
      type: "File",
      args: {
        fileId: stringArg({ required: true }),
      },
      resolve: async (_root, args, ctx) => {
        return await ctx.db.file.delete({
          where: { id: args.fileId },
        });
      },
    });
  },
});

export const UpdateFileMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.field("updateFileContent", {
      type: "File",
      args: {
        name: stringArg({ required: true }),
        extension: stringArg(),
        content: stringArg({ required: true }),
        fileId: stringArg({ required: true }),
      },
      resolve: async (_root, args, ctx) => {
        // resolve(_root, args, ctx) {
        // check if file is exist
        const file_exist = await ctx.db.file.findOne({
          where: { id: args.fileId },
        });

        if (file_exist) {
          // update content
          const result = await ctx.db.file.update({
            where: { id: args.fileId },
            data: {
              content: { set: args.content },
            },
          });
          return result;
        } else {
          // create file ,need folderId
          const new_file = {
            name: args.name,
            extention: args.extension,
            content: args.content,
            folder: { connect: { id: args.fileId } },
          };
          const result = await ctx.db.file.create({ data: new_file });
          return result;
        }
      },
    });
  },
});
